package cn.edu.sgu.www.mhxysy.service.qiling.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomDirectories;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.qiling.QilingTaozhuangDTO;
import cn.edu.sgu.www.mhxysy.entity.qiling.QilingTaozhuang;
import cn.edu.sgu.www.mhxysy.mapper.qiling.QilingTaozhuangMapper;
import cn.edu.sgu.www.mhxysy.pager.qiling.QilingTaozhuangPager;
import cn.edu.sgu.www.mhxysy.service.qiling.QilingTaozhuangService;
import cn.edu.sgu.www.mhxysy.util.ImageUpdater;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import cn.edu.sgu.www.mhxysy.util.UploadUtils;
import cn.edu.sgu.www.mhxysy.util.WrapperUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class QilingTaozhuangServiceImpl implements QilingTaozhuangService {

	private final UploadUtils uploadUtils;
	private final ImageUpdater imageUpdater;
	private final QilingTaozhuangMapper qilingTaozhuangMapper;

	@Autowired
	public QilingTaozhuangServiceImpl(
			UploadUtils uploadUtils,
			ImageUpdater imageUpdater,
			QilingTaozhuangMapper qilingTaozhuangMapper) {
		this.uploadUtils = uploadUtils;
		this.imageUpdater = imageUpdater;
		this.qilingTaozhuangMapper = qilingTaozhuangMapper;
	}

	@Override
	public String upload(MultipartFile file) throws IOException {
		String directory = CustomDirectories.DIRECTORY_QILING_TAOZHUANG;

		return uploadUtils.upload(file, directory);
	}

	@Override
	public void insert(QilingTaozhuangDTO qilingTaozhuangDTO) {
		QilingTaozhuang qilingTaozhuang = toEntity(qilingTaozhuangDTO);

		qilingTaozhuang.setId(null);

		qilingTaozhuangMapper.insert(qilingTaozhuang);
	}

	@Override
	public void updateById(QilingTaozhuangDTO qilingTaozhuangDTO) {
		ObjectUtils.requireNonNull(qilingTaozhuangDTO.getId(), CustomMessages.ID_NOT_NULL);

		QilingTaozhuang qilingTaozhuang = toEntity(qilingTaozhuangDTO);

		qilingTaozhuangMapper.updateById(qilingTaozhuang);
	}

	@Override
	public void updateImageById(Integer id, String image) {
		imageUpdater.updateImageById(image, new Consumer<Object>() {
			@Override
			public void accept(Object o) {
				UpdateWrapper<QilingTaozhuang> updateWrapper = WrapperUtils.getUpdateWrapper(id, image);

				qilingTaozhuangMapper.update(updateWrapper.getEntity(), updateWrapper);
			}
		});
	}

	@Override
	public List<QilingTaozhuang> selectAll() {
		return qilingTaozhuangMapper.selectList(null);
	}

	@Override
	public QilingTaozhuang selectById(Integer id) {
		return qilingTaozhuangMapper.selectById(id);
	}

	@Override
	public Page<QilingTaozhuang> selectByPage(QilingTaozhuangPager pager) {
		QueryWrapper<QilingTaozhuang> queryWrapper = new QueryWrapper<>();
		Page<QilingTaozhuang> page = Pager.ofPage(pager);

		queryWrapper.eq(
				pager.getType() != null,
				"type", pager.getType()
		);

		return qilingTaozhuangMapper.selectPage(page, queryWrapper);
	}

	private QilingTaozhuang toEntity(QilingTaozhuangDTO qilingTaozhuangDTO) {
		QilingTaozhuang qilingTaozhuang = new QilingTaozhuang();

		qilingTaozhuang.setId(qilingTaozhuangDTO.getId());
		qilingTaozhuang.setName(qilingTaozhuangDTO.getName());
		qilingTaozhuang.setNote(qilingTaozhuangDTO.getNote());
		qilingTaozhuang.setType(qilingTaozhuangDTO.getType());
		qilingTaozhuang.setCount(qilingTaozhuangDTO.getCount());
		qilingTaozhuang.setTemplate(qilingTaozhuangDTO.getTemplate());

		return qilingTaozhuang;
	}

}